home *** CD-ROM | disk | FTP | other *** search
/ Freelog 117 / FreelogNo117-OctobreNovembre2013.iso / Programmation / jedit / jedit5.1.0install.exe / {app} / macros / Java / Get_Class_Name.bsh < prev    next >
Text File  |  2013-07-28  |  4KB  |  128 lines

  1. /**
  2. Get_Class_Name.bsh - a BeanShell macro for the jEdit text editor replaces the 
  3. selected text with the current class name.
  4.  
  5. This macro walks though several steps in an attempt to guess the correct class
  6. name.
  7.  
  8. First, it searches backwards for the nearest occurance of the keyword 'class,' 
  9. and check which class the target belongs to, so it works for inner class definitions.
  10. If you run this macro at any point after an inner class definition but belongs to a
  11. parent class, it will return the name of the parent class.
  12.  
  13. If that fails(out of any class boundary), it attempts to guess the class name from 
  14. the file name; i.e. it would return "FooBar" if the buffer was named "FooBar.cc."  
  15. If this fails (if, for example, the buffer is untitled), it returns an empty string.
  16.  
  17. Copyright (C)  2004 Thomas Galvin - software@thomas-galvin.com
  18.  
  19. This program is free software; you can redistribute it and/or
  20. modify it under the terms of the GNU General Public License
  21. as published by the Free Software Foundation; either version 2
  22. of the License, or any later version.
  23.  
  24. This program is distributed in the hope that it will be useful,
  25. but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27. GNU General Public License for more details.
  28. */
  29.  
  30. void setCaret(int selectionStart, int selectionEnd)
  31. {
  32.   textArea.setCaretPosition(selectionStart);
  33.   textArea.moveCaretPosition(selectionEnd);
  34. }
  35.  
  36. String getClassName()
  37. {
  38.   int selectionStart;
  39.   int selectionEnd;
  40.   if(textArea.getSelection().length != 0){  // if there are selections exists
  41.       selectionStart = textArea.getSelection(0).getStart();
  42.       selectionEnd = textArea.getSelection(0).getEnd();
  43.   }
  44.   else{        // if no selection
  45.       selectionStart = textArea.getCaretPosition();
  46.       selectionEnd = textArea.getCaretPosition();
  47.   }
  48.  
  49.   String text = textArea.getText();
  50.   int index = text.lastIndexOf("class", selectionStart);
  51.  
  52.   boolean flag = false;
  53.   // check which class the targetposition belongs to
  54.   if(index != -1)
  55.   {
  56.     flag = true;
  57.     index = selectionStart;
  58.     int tag = 0;
  59.     while(flag){
  60.     char check = text.charAt(index);
  61.     if(check == '}'){
  62.         tag++;
  63.     }
  64.     if(check == '{'){
  65.         tag--;
  66.         if(tag == -1){
  67.         index = text.lastIndexOf("class", index);
  68.         break;
  69.         }
  70.     }
  71.     index--;
  72.     // out of any classes' boundary
  73.     if(index == -1)
  74.         flag = false;
  75.     }
  76.   }
  77.  
  78.   if(flag)
  79.   {
  80.     textArea.setCaretPosition(index);
  81.     int lineNumber = textArea.getCaretLine();
  82.     int lineEnd = textArea.getLineEndOffset(lineNumber);
  83.     String lineText = text.substring(index, lineEnd);   
  84.     
  85.     StringTokenizer tokenizer = new StringTokenizer(lineText);
  86.     tokenizer.nextToken(); //eat "class"
  87.     if(tokenizer.hasMoreTokens())
  88.     {
  89.       setCaret(selectionStart, selectionEnd);
  90.       return tokenizer.nextToken();
  91.     }
  92.   }
  93.   
  94.   
  95.   String fileClassName = buffer.getName();
  96.   int index = fileClassName.lastIndexOf('.');
  97.   if(index != -1)
  98.   {
  99.     fileClassName = fileClassName.substring(0, index);
  100.     if(fileClassName.toLowerCase().indexOf("untitled") == -1)
  101.     {
  102.       return fileClassName;
  103.     }
  104.   }
  105.   setCaret(selectionStart, selectionEnd);
  106.   
  107.   String className = buffer.getName();
  108.   int index = className.lastIndexOf('.');
  109.   if(index != -1)
  110.   {
  111.     className = className.substring(0, index);
  112.     if(className.toLowerCase().indexOf("untitled") == -1)
  113.     {
  114.       return className;
  115.     }
  116.   }
  117.   
  118.   return "";
  119. }
  120.  
  121. String className = getClassName();
  122. if( buffer.isReadOnly() )
  123.     Macros.error( view, "Buffer is read-only." );
  124. else if( className != null && !className.equals("") )
  125. {
  126.   textArea.setSelectedText(className);
  127. }
  128.